using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class Public_CalculatePostage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            try
            {
                SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Con"].ToString());
                con.Open();
                SqlCommand cmd = new SqlCommand("select * from Area", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        cbxSending.Items.Insert(0, dr.GetString(1));
                        cbxSending.Items[0].Value = dr.GetInt32(0).ToString();

                        cbxDestination.Items.Insert(0, dr.GetString(1));
                        cbxDestination.Items[0].Value = dr.GetInt32(0).ToString();
                    }
                }

                cmd = new SqlCommand("select Name, Fee from Service", con);
                dr.Close();
                dr = cmd.ExecuteReader();
                if (dr.HasRows)
                {
                    while (dr.Read())
                    {
                        cbxService.Items.Insert(0, dr.GetString(0));
                        cbxService.Items[0].Value = dr.GetDecimal(1).ToString();
                    }
                }
                con.Close();
            }
            catch (Exception ex)
            {
                Label7.ForeColor = System.Drawing.Color.Red;
                Label7.Text = ex.Message;
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["Con"].ToString());
        con.Open();
        SqlCommand cmd = new SqlCommand(String.Format("select Fee from Feetype where FirstArea={0} and SecondArea={1}", Int32.Parse(cbxSending.SelectedValue), Int32.Parse(cbxDestination.SelectedValue)), con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            double cost = (double.Parse(dr.GetDecimal(0).ToString()) + double.Parse(cbxService.SelectedValue)) * double.Parse(TextBox3.Text);
            cost = cost + cost / 10;
            Label7.ForeColor = System.Drawing.Color.Green;
            Label7.Text = "Total cost is " + cost + " $";
        }
        else
        {
            Label7.ForeColor = System.Drawing.Color.Red;
            Label7.Text = "Sorry. There is NOT route between " + cbxSending.SelectedItem.Text + " and " + cbxDestination.SelectedItem.Text + ".";
        }
        con.Close();
    }
}
